import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/components/health/text_ex.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/archives/archives_upgrade_attr.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/archives/archives_upgrade_title.dart';
import 'package:flutter_eve_plugin/constants/font_family.dart';
import 'package:flutter_eve_plugin/constants/font_weight.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/achieves/archieves_add_attr_entity.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/achieves/archieves_weapon_entity.dart';
import 'package:flutter_eve_plugin/pages/ye_xiu/archives/actions.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/store_vm/ye_xiu/archives/main.dart';
import 'package:flutter_eve_plugin/store_vm/ye_xiu/main.dart';
import 'package:flutter_eve_plugin/utils/image_util.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

//   武器升级界面
class ArchivesUpgradeCenterWidget extends StatelessWidget {
  final ArchievesWeaponEntity weaponEntity;

  const ArchivesUpgradeCenterWidget({Key key, this.weaponEntity})
      : super(key: key);

  List<Widget> createItem(int count) {
    return List<Widget>.generate(count, (index) {
      ArchievesWeaponUpgradegood good = weaponEntity?.upgradeGoods[index];
      if (good.goodsType == 4) {
        return Container();
      }
      return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Container(
            width: setRatio(50),
            height: setRatio(50),
            child: Stack(
              children: <Widget>[
                Container(
                  decoration: BoxDecoration(
                      image: DecorationImage(
                          image: NetworkImage(
                              '${good?.picUrl ?? '' }'
                          )),
                      borderRadius: BorderRadius.circular(setRatio(2))),
                ),
                Positioned(
                  bottom: 0,
                  child: Container(
                    height: setRatio(13),
                    width: setRatio(50),
                    decoration: BoxDecoration(
                        color: Color(0x9F000000),
                        borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(setRatio(2)),
                            bottomRight: Radius.circular(setRatio(2)))),
                    child: Center(
                      child: Text(
                        '${good?.userNum ?? 0}/${good.num ?? 0}',
                        style: TextStyle(
                            decoration: TextDecoration.none,
                            color: Color(0xFFFFFFFF),
                            fontFamily: FontFamily.PINGFANG,
                            fontWeight: FontWeightCustom.PF_Medium,
                            fontSize: setTextSp(9)),
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
          SizedBox(height: setRatio(5)),
          Text(
            '${good?.name ?? ''}',
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                decoration: TextDecoration.none,
                color: Color(0xFFEFF0F8),
                fontFamily: FontFamily.PINGFANG,
                fontWeight: FontWeightCustom.PF_Medium,
                fontSize: setTextSp(10)),
          )
        ],
      );
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(
      converter: (store) {
        return AppStateViewModel(yeXiuStateViewModel: YeXiuStateViewModel(
            archivesStateViewModel: ArchivesStateViewModel(upWeaponLevel: () {
          store.dispatch(upgradeWeapon(context));
        })));
      },
      builder: (context, vm) {
        int needGold = 0;
        for (ArchievesWeaponUpgradegood good in weaponEntity.upgradeGoods) {
//          升级所需金币
          if (good?.goodsType  == 4) {
            needGold = good.num ?? 0;
          }
        }

        ArchievesAddAttrEntity addattr;
        if (weaponEntity.nextWeaponProperty != null) {
          addattr = ArchievesAddAttrEntity(
            hp: weaponEntity.nextWeaponProperty.hp - weaponEntity.currentWeaponProperty.hp,
            def: weaponEntity.nextWeaponProperty.def - weaponEntity.currentWeaponProperty.def,
            atk: weaponEntity.nextWeaponProperty.atk - weaponEntity.currentWeaponProperty.atk,
            res: weaponEntity.nextWeaponProperty.res - weaponEntity.currentWeaponProperty.res,
          );
        } else {
          addattr = ArchievesAddAttrEntity(hp: 0,res:0,def: 0,atk: 0);
        }

        return Container(
          width: setRatio(360),
          height: setRatio(266),
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(imageUtilYeXiu(
                      'archives/icon_archivers_upgrade_center_bg@3x')))),
          child: Stack(
            overflow: Overflow.visible,
            children: <Widget>[
              ArchivesUpgradeTitleWidget(),
              ArchivesUpgradeAttrWidget(info:addattr),
              Positioned(
                top: setRatio(89),
                child: Container(
                  width: setRatio(360),
                  padding:
                      EdgeInsets.only(left: setRatio(30), right: setRatio(30)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: createItem(weaponEntity.upgradeGoods.length),
                  ),
                ),
              ),
              Positioned(
                bottom: setRatio(24),
                left: setRatio(125),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                        width: setRatio(110),
                        height: setRatio(42),
                        decoration: BoxDecoration(
                            image: DecorationImage(
                                image: AssetImage(imageUtilYeXiu(
                                    'archives/icon_archivers_upgrade@3x')))),
                        child: Padding(
                          padding: EdgeInsets.only(
                              top: setRatio(8), left: setRatio(24)),
                          child: GestureDetector(
                            onTap: () => vm.yeXiuStateViewModel
                                .archivesStateViewModel.upWeaponLevel(),
                            child: TextEx(
                              '提升一级',
                              shadow: Color(0xCCCB8829),
                              style: TextStyle(
                                  fontFamily: FontFamily.PINGFANG,
                                  fontWeight: FontWeightCustom.PF_Medium,
                                  fontSize: setTextSp(15),
                                  color: Color(0xFFFFFFFF)),
                            ),
                          ),
                        )),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Image.asset(
                          imageUtilYeXiu('main/icon_nav_right_coin@3x'),
                          width: setRatio(20),
                          height: setRatio(20),
                        ),
                        Text(
                          '$needGold',
                          style: TextStyle(
                              decoration: TextDecoration.none,
                              fontFamily: FontFamily.PINGFANG,
                              fontWeight: FontWeightCustom.PF_Medium,
                              fontSize: setTextSp(13),
                              color: Color(0xFFFFFFFF)),
                        )
                      ],
                    )
                  ],
                ),
              )
            ],
          ),
        );
      },
    );
  }
}
